package com.aiyige.page.publish.util;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.aiyige.MyApp;
import com.aiyige.base.db.DBHelper;
import com.aiyige.base.db.dao.InfoModelDao;
import com.aiyige.base.db.dao.LearnVideoModelDao;
import com.aiyige.base.db.dao.MajorCourseDBModelDao;
import com.aiyige.base.db.dao.NormalVideoModelDao;
import com.aiyige.base.db.dao.PhotoModelDao;
import com.aiyige.base.db.dao.SpecialTopicDBModelDao;
import com.aiyige.base.db.dao.TraingCardDBModelDao;
import com.aiyige.base.db.dao.TraingClassDBModelDao;
import com.aiyige.base.db.dao.UploadFileDao;
import com.aiyige.base.db.table.InfoModel;
import com.aiyige.base.db.table.LearnVideoModel;
import com.aiyige.base.db.table.MajorCourseDBModel;
import com.aiyige.base.db.table.NormalVideoModel;
import com.aiyige.base.db.table.PhotoModel;
import com.aiyige.base.db.table.SpecialTopicDBModel;
import com.aiyige.base.db.table.TraingCardDBModel;
import com.aiyige.base.db.table.TraingClassDBModel;
import com.aiyige.base.db.table.UploadFile;
import com.aiyige.base.eventbus.EventCopyInfoFinish;
import com.aiyige.base.eventbus.EventCopyMajorCourseFinish;
import com.aiyige.base.eventbus.EventCopySpecialTopicFinish;
import com.aiyige.base.eventbus.EventCopyTraingCardFinish;
import com.aiyige.base.eventbus.EventCopyTraingClassFinish;
import com.aiyige.base.eventbus.EventEditInfoFinish;
import com.aiyige.base.eventbus.EventEditLearnVideoFinish;
import com.aiyige.base.eventbus.EventEditMajorCourseFinish;
import com.aiyige.base.eventbus.EventEditSpecialTopicFinish;
import com.aiyige.base.eventbus.EventEditTraingCardFinish;
import com.aiyige.base.eventbus.EventEditTraingClassFinish;
import com.aiyige.model.moment.backup.DetailBackup;
import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.moment.entity.Content;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.Photo;
import com.aiyige.model.moment.entity.SinglePhoto;
import com.aiyige.model.moment.entity.SingleVideo;
import com.aiyige.model.moment.entity.Video;
import com.aiyige.model.moment.entity.VideoCourse;
import com.aiyige.page.my.dynamicmanagement.DMPage;
import com.aiyige.page.my.merchandisemanagement.MMPage;
import com.aiyige.page.publish.coursedescription.model.CourseCover;
import com.aiyige.page.publish.info.model.PublishInfoFormModel;
import com.aiyige.page.publish.info.model.TextNode;
import com.aiyige.page.publish.learnvideo.model.PublishLearnVideoFormModel;
import com.aiyige.page.publish.learnvideo.model.PublishLearnVideoItem;
import com.aiyige.page.publish.majorcourse.model.MajorCourseFormModel;
import com.aiyige.page.publish.normalvideo.model.PublishNormalVideoFormModel;
import com.aiyige.page.publish.normalvideo.model.PublishNormalVideoItem;
import com.aiyige.page.publish.photo.model.PhotoFormModel;
import com.aiyige.page.publish.photo.model.PublishPhotoItem;
import com.aiyige.page.publish.service.PublishService;
import com.aiyige.page.publish.specialtopic.model.SpecialTopicFormModel;
import com.aiyige.page.publish.traingcard.model.TraingCardFormModel;
import com.aiyige.page.publish.traingclass.model.TraingClassFormModel;
import com.aiyige.richtexteditor.util.RichTextUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.FileUtil;
import com.aiyige.utils.UUIDUtil;
import com.aiyige.utils.widget.articleview.model.ArticleNode;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.j256.ormlite.misc.TransactionManager;

import org.greenrobot.eventbus.EventBus;

import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created by zhuyakun on 2017/11/7.
 */

public class PublishUtil {
    public static final int MAX_INFO_VIDEO_PHOTO_NUM = 99;
    public static final int MAX_VIDEO_TITLE_LENGTH = 30;
    public static final int MAX_PHOTO_DESC_LENGTH = 50;
    public static final long MAX_PUBLISH_VIDEO_SIZE = 1024 * 1024 * 1024;
    public static final int MAX_PUBLISH_PHOTO_NUM = 9;
    public static final int MAX_PUBLISH_LEARN_TARGET_NUM = 3;
    public static final int MAX_PUBLISH_LEARN_AGE_NUM = 3;
    public static final int MAX_PUBLISH_TAG_NUM = 3;
    public static final int MAX_PUBLISH_NORMAL_VIDEO_NUM = 9;
    public static final int MAX_PUBLISH_LEARN_VIDEO_NUM = 99;
    public static final int MAX_COURSE_DESCRIPTION_COVER_NUM = 9;
    public static final double MIN_PLATFORM_DIVIDEND = 0.03;
    public static final double MIN_LEARN_VIDEO_PLATFORM_DIVIDEND = 0.5;

    public static final long SCAN_INTERVAL_MS = 1000;
    public static final long UPLOAD_IDLE_TIMEOUT_MS = 3 * 60 * 1000;
    public static final int MAX_UPLOAD_RETRY_NUM = 5;
    public static final int MAX_PUBLISH_RETRY_NUM = 3;


    public static final int CONTROL_OPTION_UPLOAD = 1;
    public static final int CONTROL_OPTION_PAUSE = 2;
    public static final int CONTROL_OPTION_DELETE = 3;
    public static final int CONTROL_OPTION_FAILED_PAUSE = 4;

    public static final int PROGRESS_STATUS_NEED_TRANSCODE = 1;
    public static final int PROGRESS_STATUS_TRANSCODE_FINISH = 2;
    public static final int PROGRESS_STATUS_TRANSCODE_FAILED = 3;
    public static final int PROGRESS_STATUS_WAITING_UPLOAD = 4;
    public static final int PROGRESS_STATUS_UPLOADING = 5;
    public static final int PROGRESS_STATUS_UPLOAD_FINISH = 7;
    public static final int PROGRESS_STATUS_PRE_UPLOAD_FINISH = 15;
    public static final int PROGRESS_STATUS_SUBMITTING = 8;
    public static final int PROGRESS_STATUS_SUBMIT_FAILED = 10;
    public static final int PROGRESS_STATUS_NOT_PUBLISHED = 11;
    public static final int PROGRESS_STATUS_IN_REVIEW = 12;
    public static final int PROGRESS_STATUS_PUBLISHED = 13;
    public static final int PROGRESS_STATUS_REJECT = 14;
    public static final int PROGRESS_STATUS_SERVER_TRANSCODING = 16;

    public static final int BUSINESS_TYPE_FREE = 1;
    public static final int BUSINESS_TYPE_TRAIL = 2;
    public static final int BUSINESS_TYPE_PAID = 3;

    public static final int PARENT_TYPE_LEARN_VIDEO = 1;
    public static final int PARENT_TYPE_NORMAL_VIDEO = 2;
    public static final int PARENT_TYPE_PHOTO = 3;
    public static final int PARENT_TYPE_INFO = 4;
    public static final int PARENT_TYPE_MAJOR_COURESE = 5;
    public static final int PARENT_TYPE_TRAING_CLASS = 6;
    public static final int PARENT_TYPE_TRAING_CARD = 7;
    public static final int PARENT_TYPE_SPECIAL_TOPIC = 8;

    public static final int UPLOAD_FILE_TYPE_VIDEO = 1;
    public static final int UPLOAD_FILE_TYPE_PHOTO = 2;

    public static final int PUBLISH_OPERATION_TYPE_SAVE = 1;
    public static final int PUBLISH_OPERATION_TYPE_SUBMIT_REVIEW = 2;
    public static final int PUBLISH_OPERATION_TYPE_EDIT_SAVE = 3;
    public static final int PUBLISH_OPERATION_TYPE_EDIT_SUBMIT_REIVEW = 4;
    public static final int PUBLISH_OPERATION_TYPE_COPY_SAVE = 5;
    public static final int PUBLISH_OPERATION_TYPE_COPY_SUBMIT_REIVEW = 6;

    public static final int OPERATION_TYPE_CREATE = 1;
    public static final int OPERATION_TYPE_EDIT = 2;
    public static final int OPERATION_TYPE_COPY = 3;

    public static final String ACTION_UPLOAD_UPDATE = "com.aiyige.action.ACTION_UPLOAD_UPDATE";
    public static final String ACTION_PUBLISH_PROGRESS_STATUS_UPDATE = "com.aiyige.action.ACTION_PUBLISH_PROGRESS_STATUS_UPDATE";
    public static final String ACTION_TRANSCODE_UPDATE = "com.aiyige.action.ACTION_TRANSCODE_UPDATE";

    public static final String EXTRA_KEY_PUBLISH_DIALOG_TYPE = "com.aiyige.extra.EXTRA_KEY_PUBLISH_DIALOG_TYPE";
    public static final String PUBLISH_DIALOG_TYPE_SUBMIT_REVIEW_DIALOG = "submitReviewDialog";
    public static final String PUBLISH_DIALOG_TYPE_SAVE_DIALOG = "saveDialog";

    public static final int PUBLISH_SOURCE_ANDROID = 1;
    public static final int PUBLISH_SOURCE_IOS = 2;
    public static final int PUBLISH_SOURCE_WEB_BROWSER = 3;
    public static final int PUBLISH_SOURCE_UNKNOWN = 4;

    public static void sendUploadUpdateBroadcast() {
        MyApp.getAppContext().sendBroadcast(new Intent(ACTION_UPLOAD_UPDATE));
    }

    public static void sendPublishProgressStatusUpdateBroadcast() {
        MyApp.getAppContext().sendBroadcast(new Intent(ACTION_PUBLISH_PROGRESS_STATUS_UPDATE));
    }

    public static void sendTranscodeUpdateBroadcast() {
        MyApp.getAppContext().sendBroadcast(new Intent(ACTION_TRANSCODE_UPDATE));
    }


    public static void startPublish() {
        PublishService.startPublish(MyApp.getAppContext());
    }

    public static void pauseAll() {
        UploadFileDao.pauseAll();
        PhotoModelDao.pauseAll();
        NormalVideoModelDao.pauseAll();
        LearnVideoModelDao.pauseAll();
        InfoModelDao.pauseAll();
        MajorCourseDBModelDao.pauseAll();
        TraingClassDBModelDao.pauseAll();
        TraingCardDBModelDao.pauseAll();
        SpecialTopicDBModelDao.pauseAll();
    }

    public static String extractLocalIdFromMultiItemEntity(MultiItemEntity multiItemEntity) {
        String id = "";
        try {
            switch (multiItemEntity.getItemType()) {
                case PublishUtil.PARENT_TYPE_INFO:
                    id = ((InfoModel) multiItemEntity).getId();
                    break;
                case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                    id = ((LearnVideoModel) multiItemEntity).getId();
                    break;
                case PublishUtil.PARENT_TYPE_MAJOR_COURESE:
                    id = ((MajorCourseDBModel) multiItemEntity).getId();
                    break;
                case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                    id = ((NormalVideoModel) multiItemEntity).getId();
                    break;
                case PublishUtil.PARENT_TYPE_PHOTO:
                    id = ((PhotoModel) multiItemEntity).getId();
                    break;
                case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                    id = ((SpecialTopicDBModel) multiItemEntity).getId();
                    break;
            }
        } catch (Exception e) {

        }
        return id;
    }

    public static String extractServerIdFromMultiItemEntity(MultiItemEntity multiItemEntity) {
        String id = "";
        try {
            switch (multiItemEntity.getItemType()) {
                case PublishUtil.PARENT_TYPE_INFO:
                    id = ((InfoModel) multiItemEntity).getGoodsId();
                    break;
                case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                    id = ((SpecialTopicDBModel) multiItemEntity).getGoodsId();
                    break;
                case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                    id = ((LearnVideoModel) multiItemEntity).getGoodsId();
                    break;
                case PublishUtil.PARENT_TYPE_MAJOR_COURESE:
                    id = ((MajorCourseDBModel) multiItemEntity).getGoodsId();
                    break;
                case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                    id = ((NormalVideoModel) multiItemEntity).getGoodsId();
                    break;
                case PublishUtil.PARENT_TYPE_PHOTO:
                    id = ((PhotoModel) multiItemEntity).getGoodsId();
                    break;
            }
        } catch (Exception e) {

        }
        return id;
    }


    public static void resumeParent(int parentType, String parentId) {
        switch (parentType) {
            case PARENT_TYPE_INFO:
                InfoModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_LEARN_VIDEO:
                LearnVideoModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_MAJOR_COURESE:
                MajorCourseDBModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_NORMAL_VIDEO:
                NormalVideoModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_PHOTO:
                PhotoModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_TRAING_CLASS:
                TraingClassDBModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_TRAING_CARD:
                TraingCardDBModelDao.resumeById(parentId);
                break;
            case PARENT_TYPE_SPECIAL_TOPIC:
                SpecialTopicDBModelDao.resumeById(parentId);
                break;
        }
    }

    public static void pauseParent(int parentType, String parentId) {
        switch (parentType) {
            case PARENT_TYPE_INFO:
                InfoModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_LEARN_VIDEO:
                LearnVideoModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_MAJOR_COURESE:
                MajorCourseDBModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_NORMAL_VIDEO:
                NormalVideoModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_PHOTO:
                PhotoModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_TRAING_CLASS:
                TraingClassDBModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_TRAING_CARD:
                TraingCardDBModelDao.pauseById(parentId);
                break;
            case PARENT_TYPE_SPECIAL_TOPIC:
                SpecialTopicDBModelDao.pauseById(parentId);
                break;
        }
    }

    public static void deleteParent(int parentType, String parentId) {
        switch (parentType) {
            case PARENT_TYPE_INFO:
                InfoModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_LEARN_VIDEO:
                LearnVideoModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_MAJOR_COURESE:
                MajorCourseDBModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_NORMAL_VIDEO:
                NormalVideoModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_PHOTO:
                PhotoModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_TRAING_CLASS:
                TraingClassDBModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_TRAING_CARD:
                TraingCardDBModelDao.deleteById(parentId);
                break;
            case PARENT_TYPE_SPECIAL_TOPIC:
                SpecialTopicDBModelDao.deleteById(parentId);
                break;
        }
    }

    public static String generatePhotoFileStoreKey(String filePath) {
        return generatePhotoFileStoreKey("", filePath);
    }

    public static String generatePhotoFileStoreKey(String uuid, String filePath) {
        String suffix = "";
//        try {
//            suffix = filePath.substring(filePath.lastIndexOf("."));
//            suffix = URLEncoder.encode(suffix, "UTF-8");
//        } catch (Exception e) {
//            suffix = "";
//        }
        return "photo/" + AccountUtil.getCurrentUser().getId() + "_" + System.currentTimeMillis() + "_" + (TextUtils.isEmpty(uuid) ? UUIDUtil.generate() : uuid) + suffix;
    }

    public static String generateVideoFileStoreKey(String filePath) {
        return generateVideoFileStoreKey("", filePath);
    }

    public static String generateVideoFileStoreKey(String uuid, String filePath) {
        String suffix = "";
//        try {
//            suffix = filePath.substring(filePath.lastIndexOf("."));
//            suffix = URLEncoder.encode(suffix, "UTF-8");
//        } catch (Exception e) {
//            suffix = "";
//        }
        return "video/" + AccountUtil.getCurrentUser().getId() + "_" + System.currentTimeMillis() + "_" + (TextUtils.isEmpty(uuid) ? UUIDUtil.generate() : uuid) + suffix;
    }


    public static Moment convertPhotoFormModelToMoment(PhotoFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_PHOTO);
        moment.setTitle(formModel.getTitle());
        moment.setSummary(formModel.getIntroduction());
        List<SinglePhoto> photoList = new LinkedList<>();
        for (PublishPhotoItem item : formModel.getPublishPhotoItemList()) {
            SinglePhoto singlePhoto = new SinglePhoto();
            singlePhoto.setTitle(item.getIntroduction());
            photoList.add(singlePhoto);
        }
        Photo photo = new Photo();
        photo.setPhotos(photoList);
        photo.setPhotoCount(photoList.size());
        moment.setMoreBackup(JSON.toJSONString(photo));
        return moment;
    }

    public static Moment convertInfoFormModelToMoment(PublishInfoFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_ARTICLE);
        moment.setTitle(formModel.getTitle());
        List<Content> contentList = PublishUtil.convertArticleToContent("", "", formModel.getArticleNodeList());
        DetailBackup detailBackup = new DetailBackup();
        detailBackup.setContent(contentList);
        moment.setDetailBackup(detailBackup);
        return moment;
    }

    public static Moment convertSpecialTopicFormModelToMoment(SpecialTopicFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_SPECIAL_TOPIC);
        moment.setTitle(formModel.getTitle());
        List<Content> contentList = PublishUtil.convertArticleToContent("", "", formModel.getArticleNodeList());
        DetailBackup detailBackup = new DetailBackup();
        detailBackup.setContent(contentList);
        moment.setDetailBackup(detailBackup);
        return moment;
    }


    public static Moment convertNormalVideoFormModelToMoment(PublishNormalVideoFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_VIDEO);
        moment.setTitle(formModel.getTitle());
        moment.setSummary(formModel.getCourseIntroduction());
        List<SingleVideo> videoList = new LinkedList<>();
        for (PublishNormalVideoItem item : formModel.getPublishNormalVideoItemList()) {
            SingleVideo singleVideo = new SingleVideo();
            singleVideo.setTitle(item.getTitle());
            videoList.add(singleVideo);
        }
        Video video = new Video();
        video.setVideos(videoList);
        moment.setMoreBackup(JSON.toJSONString(video));
        return moment;
    }

    public static int convertMomentStatusToProgressStatus(int momentStatus) {
        int progressStatus;
        switch (momentStatus) {
            case Moment.STATUS_IN_REVIEW:
                progressStatus = PublishUtil.PROGRESS_STATUS_IN_REVIEW;
                break;
            case Moment.STATUS_REJECT:
                progressStatus = PublishUtil.PROGRESS_STATUS_REJECT;
                break;
            case Moment.STATUS_PUT_ON:
                progressStatus = PublishUtil.PROGRESS_STATUS_PUBLISHED;
                break;
            case Moment.STATUS_SAVE:
                progressStatus = PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED;
                break;
            case Moment.STATUS_REVIEW_PASSED:
                progressStatus = PublishUtil.PROGRESS_STATUS_PUBLISHED;
                break;
            case Moment.STATUS_GET_OFF:
                progressStatus = PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED;
                break;
            case Moment.STATUS_TRANSCODING:
                progressStatus = PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING;
                break;
            default:
                progressStatus = PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED;
                break;
        }
        return progressStatus;
    }

    public static Moment convertLearnVideoFormModelToMoment(PublishLearnVideoFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_VIDEO_COURSE);
        moment.setTitle(formModel.getTitle());
        moment.setSummary(formModel.getCourseIntroduction());
        List<SingleVideo> videoList = new LinkedList<>();
        for (PublishLearnVideoItem item : formModel.getPublishLearnVideoItemList()) {
            SingleVideo singleVideo = new SingleVideo();
            singleVideo.setTitle(item.getTitle());
            videoList.add(singleVideo);
        }
        VideoCourse videoCourse = new VideoCourse();
        videoCourse.setLearningGoals(formModel.getLearnTarget());
        videoCourse.setSuitablePopulation(formModel.getSuitablePeople());
        videoCourse.setCourseType(formModel.getCourseType());
        videoCourse.setTeacher(formModel.getTeacher());
        videoCourse.setVideos(videoList);
        moment.setMoreBackup(JSON.toJSONString(videoCourse));
        return moment;
    }

    public static Moment convertMajorCourseFormModelToMoment(MajorCourseFormModel formModel) {
        Moment moment = new Moment();
        moment.setSubject(Moment.SUBJECT_MAJOR_COURSE);
        moment.setTitle(formModel.getTitle());
        MajorCourse majorCourse = new MajorCourse();
        List<Content> courseScheduleContentList = PublishUtil.convertArticleToContent("", "", formModel.getCourseScheduleModel().getArticleNodeList());
        DetailBackup detailBackup = new DetailBackup();
        majorCourse.setTimetable(courseScheduleContentList);
        List<Content> coverList = PublishUtil.convertCourseCoverToContent("", "", formModel.getCourseDescriptionModel().getCourseCoverList());
        majorCourse.setCoverSet(coverList);
        List<Content> coruseDescContentList = PublishUtil.convertArticleToContent("", "", formModel.getCourseDescriptionModel().getArticleNodeList());
        detailBackup.setContent(coruseDescContentList);
        majorCourse.setSuitablePopulation(formModel.getSuitablePeople());
        majorCourse.setLearningAges(formModel.getSuitableLearnAge());
        majorCourse.setLearningGoals(formModel.getLearnTarget());
        majorCourse.setRichText(formModel.getCourseDescriptionModel().getRteModel().getContent());
        majorCourse.setTimetableRichText(formModel.getCourseScheduleModel().getRteModel().getContent());
        moment.setMoreBackup(JSON.toJSONString(majorCourse));
        moment.setDetailBackup(detailBackup);
        return moment;
    }


    public static List<CourseCover> convertContentToCourseCover(List<Content> contentList) {
        List<CourseCover> courseCoverList = new LinkedList<>();
        for (Content content : contentList) {
            switch (content.getType()) {
                case Content.TYPE_PHOTO: {
                    courseCoverList.add(CourseCover.newBuilder()
                            .type(CourseCover.TYPE_NORMAL)
                            .mediaType(CourseCover.MEDIA_TYPE_PHOTO)
                            .imageUrl(content.getUrl())
                            .defaultCover(content.getDefaultMedia() == null ? false : content.getDefaultMedia())
                            .needUpload(false)
                            .build());
                    break;
                }
                case Content.TYPE_VIDEO: {
                    courseCoverList.add(CourseCover.newBuilder()
                            .type(CourseCover.TYPE_NORMAL)
                            .mediaType(CourseCover.MEDIA_TYPE_VIDEO)
                            .imageUrl(content.getCover())
                            .videoUrl(content.getUrl())
                            .defaultCover(content.getDefaultMedia() == null ? false : content.getDefaultMedia())
                            .needUpload(false)
                            .build());
                    break;
                }
            }
        }
        return courseCoverList;
    }

    public static List<Content> convertCourseCoverToContent(String photoDomain, String videoDomain, List<CourseCover> courseCoverList) {
        List<Content> contentList = new LinkedList<>();
        for (CourseCover courseCover : courseCoverList) {
            if (courseCover.isNeedUpload()) {
                try {
                    UploadFile uploadFile = UploadFileDao.getDao().queryForId(courseCover.getUploadFileId());
                    Content content = new Content();
                    content.setDefaultMedia(courseCover.isDefaultCover());
                    switch (uploadFile.getFileType()) {
                        case PublishUtil.UPLOAD_FILE_TYPE_PHOTO:
                            content.setType(Content.TYPE_PHOTO);
                            content.setText(uploadFile.getDescription());
                            content.setUrl(photoDomain + uploadFile.getStoreKey());
                            break;
                        case PublishUtil.UPLOAD_FILE_TYPE_VIDEO:
                            content.setType(Content.TYPE_VIDEO);
                            content.setPersistentId(uploadFile.getPersistentId());
                            content.setText(uploadFile.getDescription());
                            content.setUrl(videoDomain + uploadFile.getStoreKey());
                            content.setCover(photoDomain + uploadFile.getCoverStoreKey());
                            break;
                    }
                    contentList.add(content);
                } catch (Exception e) {

                }
            } else {
                Content content = new Content();
                content.setDefaultMedia(courseCover.isDefaultCover());
                switch (courseCover.getMediaType()) {
                    case CourseCover.MEDIA_TYPE_PHOTO:
                        content.setType(Content.TYPE_PHOTO);
                        content.setUrl(courseCover.getImageUrl());
                        break;
                    case CourseCover.MEDIA_TYPE_VIDEO:
                        content.setType(Content.TYPE_VIDEO);
                        content.setUrl(courseCover.getVideoUrl());
                        content.setCover(courseCover.getImageUrl());
                        break;
                }
                contentList.add(content);
            }
        }
        return contentList;
    }


    public static List<ArticleNode> convertContentToArticle(List<Content> contentList) {
        List<ArticleNode> articleNodeList = new LinkedList<>();
        for (Content content : contentList) {
            switch (content.getType()) {
                case Content.TYPE_PHOTO: {
                    articleNodeList.add(ArticleNode.newBuilder()
                            .type(ArticleNode.NODE_TYPE_PHOTO)
                            .url(content.getUrl())
                            .needUpload(false)
                            .text(content.getTitle())
                            .expand(!TextUtils.isEmpty(content.getTitle()))
                            .build());
                    break;
                }
                case Content.TYPE_TEXT: {
                    int textStyle;
                    switch (content.getFontStyle()) {
                        case Content.FONT_STYLE_BIG_TITLE:
                            textStyle = ArticleNode.TEXT_STYLE_BIG_TITLE;
                            break;
                        case Content.FONT_STYLE_MAIN_BODY:
                            textStyle = ArticleNode.TEXT_STYLE_MAIN_BODY;
                            break;
                        case Content.FONT_STYLE_SMALL_TITLE:
                            textStyle = ArticleNode.TEXT_STYLE_SMALL_TITLE;
                            break;
                        default:
                            textStyle = ArticleNode.TEXT_STYLE_SMALL_TITLE;
                            break;
                    }
                    articleNodeList.add(ArticleNode.newBuilder()
                            .text(content.getText())
                            .type(ArticleNode.NODE_TYPE_TEXT)
                            .textStyle(textStyle)
                            .build());
                    break;
                }
                case Content.TYPE_VIDEO: {
                    articleNodeList.add(ArticleNode.newBuilder()
                            .url(content.getUrl())
                            .coverUrl(content.getCover())
                            .needUpload(false)
                            .type(ArticleNode.NODE_TYPE_VIDEO)
                            .build());
                    break;
                }
            }
        }
        return articleNodeList;
    }


    public static List<Content> convertArticleToContent(String photoDomain, String videoDomain, List<ArticleNode> articleNodeList) {
        List<Content> contentList = new LinkedList<>();
        for (ArticleNode articleNode : articleNodeList) {
            switch (articleNode.getType()) {
                case ArticleNode.NODE_TYPE_TEXT: {
                    Content content = new Content();
                    content.setType(Content.TYPE_TEXT);
                    switch (articleNode.getTextStyle()) {
                        case TextNode.TEXT_STYLE_BIG_TITLE:
                            content.setFontStyle(Content.FONT_STYLE_BIG_TITLE);
                            break;
                        case TextNode.TEXT_STYLE_SMALL_TITLE:
                            content.setFontStyle(Content.FONT_STYLE_SMALL_TITLE);
                            break;
                        case TextNode.TEXT_STYLE_MAIN_BODY:
                            content.setFontStyle(Content.FONT_STYLE_MAIN_BODY);
                            break;
                    }
                    content.setText(articleNode.getText());
                    contentList.add(content);
                    break;
                }
                case ArticleNode.NODE_TYPE_PHOTO: {
                    if (articleNode.isNeedUpload()) {
                        try {
                            UploadFile photo = UploadFileDao.getDao().queryForId(articleNode.getUploadFileId());
                            Content content = new Content();
                            content.setTitle(photo.getDescription());
                            content.setType(Content.TYPE_PHOTO);
                            content.setUrl(photoDomain + photo.getStoreKey());
                            contentList.add(content);
                        } catch (Exception e) {

                        }
                    } else {
                        Content content = new Content();
                        content.setText(articleNode.getText());
                        content.setType(Content.TYPE_PHOTO);
                        content.setUrl(articleNode.getUrl());
                        contentList.add(content);
                    }
                    break;
                }
                case ArticleNode.NODE_TYPE_VIDEO: {
                    if (articleNode.isNeedUpload()) {
                        try {
                            UploadFile video = UploadFileDao.getDao().queryForId(articleNode.getUploadFileId());
                            Content content = new Content();
                            content.setType(Content.TYPE_VIDEO);
                            content.setPersistentId(video.getPersistentId());
                            content.setCover(photoDomain + video.getCoverStoreKey());
                            content.setUrl(videoDomain + video.getStoreKey());
                            contentList.add(content);
                        } catch (Exception e) {

                        }
                    } else {
                        Content content = new Content();
                        content.setType(Content.TYPE_VIDEO);
                        content.setCover(articleNode.getCoverUrl());
                        content.setUrl(articleNode.getUrl());
                        contentList.add(content);
                    }
                    break;
                }
            }
        }
        return contentList;
    }

    public static LearnVideoModel publishLearnVideo(final int publishOperationType, final PublishLearnVideoFormModel formModel) throws SQLException {
        LearnVideoModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<LearnVideoModel>() {
            @Override
            public LearnVideoModel call() throws Exception {
                long releaseDate = System.currentTimeMillis();
                LearnVideoModel learnVideoModel = LearnVideoModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .goodsId(formModel.getGoodsId())
                        .version(formModel.getVersion())
                        .platformDividend(formModel.getPlatformDividend())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .releaseDate(releaseDate)
                        .sellDate(releaseDate)
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .courseType(formModel.getCourseType())
                        .learnTarget(formModel.getLearnTarget())
                        .suitablePeople(formModel.getSuitablePeople())
                        .publishOperationType(publishOperationType)
                        .introduction(formModel.getCourseIntroduction())
                        .interest(JSON.toJSONString(formModel.getInterestList()))
                        .industry(JSON.toJSONString(formModel.getIndustryList()))
                        .price(Double.parseDouble(formModel.getPrice()))
                        .priceId(formModel.getPriceId())
                        .title(formModel.getTitle())
                        .teacher(formModel.getTeacher())
                        .location(JSON.toJSONString(formModel.getLocationData()))
                        .guarantee(JSON.toJSONString(formModel.getSelectGuaranteeItemList()))
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .videoNum(formModel.getPublishLearnVideoItemList().size())
                        .build();
                UploadFile learnVideoCover = UploadFile.newBuilder()
                        .userId(AccountUtil.getCurrentUser().getId())
                        .fileLocalUrl(formModel.getCoverUrl())
                        .storeKey(PublishUtil.generatePhotoFileStoreKey(formModel.getCoverUrl()))
                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                        .parentId(learnVideoModel.getId())
                        .parentType(PublishUtil.PARENT_TYPE_LEARN_VIDEO)
                        .totalSize(FileUtil.getFileSize(formModel.getCoverUrl()))
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                        .build();
                UploadFileDao.getDao().create(learnVideoCover);
                learnVideoModel.setCoverStoreKey(learnVideoCover.getStoreKey());
                learnVideoModel.setCoverUrl(learnVideoCover.getFileLocalUrl());
                LearnVideoModelDao.getDao().create(learnVideoModel);
                List<PublishLearnVideoItem> videoItemList = formModel.getPublishLearnVideoItemList();
                for (PublishLearnVideoItem videoItem : videoItemList) {
                    if (!videoItem.isPreUpload()) {
                        UploadFile videoCover = UploadFile.newBuilder()
                                .userId(AccountUtil.getCurrentUser().getId())
                                .fileLocalUrl(videoItem.getCoverUrl())
                                .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                .storeKey(PublishUtil.generatePhotoFileStoreKey(videoItem.getCoverUrl()))
                                .parentId(learnVideoModel.getId())
                                .parentType(PublishUtil.PARENT_TYPE_LEARN_VIDEO)
                                .totalSize(FileUtil.getFileSize(videoItem.getCoverUrl()))
                                .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                .build();
                        UploadFileDao.getDao().create(videoCover);
                        UploadFile video = UploadFile.newBuilder()
                                .userId(AccountUtil.getCurrentUser().getId())
                                .fileLocalUrl(videoItem.getVideoUrl())
                                .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                .bussinessType(videoItem.isTrail() ? PublishUtil.BUSINESS_TYPE_TRAIL : PublishUtil.BUSINESS_TYPE_PAID)
                                .storeKey(PublishUtil.generateVideoFileStoreKey(videoItem.getVideoUrl()))
                                .parentId(learnVideoModel.getId())
                                .parentType(PublishUtil.PARENT_TYPE_LEARN_VIDEO)
                                .totalSize(videoItem.getSize())
                                .duration(videoItem.getDuration())
                                .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                .title(videoItem.getTitle())
                                .coverStoreKey(videoCover.getStoreKey())
                                .coverUrl(videoCover.getFileLocalUrl())
                                .build();
                        UploadFileDao.getDao().create(video);
                    } else {
                        UploadFile video = UploadFile.newBuilder()
                                .userId(AccountUtil.getCurrentUser().getId())
                                .fileLocalUrl(videoItem.getVideoUrl())
                                .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                .bussinessType(videoItem.isTrail() ? PublishUtil.BUSINESS_TYPE_TRAIL : PublishUtil.BUSINESS_TYPE_PAID)
                                .parentId(learnVideoModel.getId())
                                .parentType(PublishUtil.PARENT_TYPE_LEARN_VIDEO)
                                .totalSize(videoItem.getSize())
                                .duration(videoItem.getDuration())
                                .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                .progressStatus(PublishUtil.PROGRESS_STATUS_PRE_UPLOAD_FINISH)
                                .title(videoItem.getTitle())
                                .coverUrl(videoItem.getCoverUrl())
                                .build();
                        UploadFileDao.getDao().create(video);
                    }
                }
                return learnVideoModel;
            }
        });
        PublishUtil.startPublish();
        if (formModel.getOperationType() == PublishUtil.OPERATION_TYPE_EDIT) {
            EventBus.getDefault().post(EventEditLearnVideoFinish.newBuilder()
                    .learnVideoModel(result)
                    .resultCode(EventEditLearnVideoFinish.RESULT_OK)
                    .build());
        }
        return result;

    }

    public static MajorCourseDBModel publishMajorCourse(final int publishOperationType, final MajorCourseFormModel majorCourseFormModel) throws Exception {
        MajorCourseDBModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<MajorCourseDBModel>() {
            @Override
            public MajorCourseDBModel call() throws Exception {
                long releaseDate = System.currentTimeMillis();
                MajorCourseDBModel majorCourseDBModel = MajorCourseDBModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .releaseDate(releaseDate)
                        .sellDate(releaseDate)
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .title(majorCourseFormModel.getTitle())
                        .goodsId(majorCourseFormModel.getGoodsId())
                        .courseNumber(majorCourseFormModel.getCourseNumber())
                        .classLocation(JSON.toJSONString(majorCourseFormModel.getClassLocation()))
                        .classSize(majorCourseFormModel.getClassSize())
                        .confirmDate(majorCourseFormModel.getConfirmDate())
                        .coverUrl(majorCourseFormModel.getCover().getImageUrl())
                        .coverNeedUpload(majorCourseFormModel.getCover().isNeedUpload())
                        .endClassDate(majorCourseFormModel.getEndClassDate())
                        .guarantee(JSON.toJSONString(majorCourseFormModel.getGuaranteeItemList()))
                        .interest(JSON.toJSONString(majorCourseFormModel.getInterestList()))
                        .industry(JSON.toJSONString(majorCourseFormModel.getIndustryList()))
                        .learnTarget(majorCourseFormModel.getLearnTarget())
                        .minClassSize(majorCourseFormModel.getMinClassSize())
                        .openBuy(majorCourseFormModel.getOpenBuy())
                        .openClassCondition(majorCourseFormModel.getOpenClassCondition())
                        .platformDividend(majorCourseFormModel.getPlatformDividend())
                        .price(majorCourseFormModel.getPrice())
                        .publishOperationType(publishOperationType)
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .retryCount(0)
                        .router("")
                        .majorCourseType(JSON.toJSONString(majorCourseFormModel.getMajorCourseType()))
                        .singleClassDuration(majorCourseFormModel.getSingleClassDuration())
                        .singlePrice(majorCourseFormModel.getSinglePrice())
                        .startClassDate(majorCourseFormModel.getStartClassDate())
                        .suitableLearnAge(majorCourseFormModel.getSuitableLearnAge())
                        .suitablePeople(majorCourseFormModel.getSuitablePeople())
                        .traingCard(JSON.toJSONString(majorCourseFormModel.getTraingCardList()))
                        .joinConsumptionSecurityPlan(majorCourseFormModel.isJoinConsumptionSecurityPlan())
                        .uploadedPercent(0)
                        .build();
                RichTextUtil.uploadRTELocalMedia(PublishUtil.PARENT_TYPE_MAJOR_COURESE, majorCourseDBModel.getId(), majorCourseFormModel.getCourseScheduleModel().getRteModel().getRteLocalMediaList());
                RichTextUtil.uploadRTELocalMedia(PublishUtil.PARENT_TYPE_MAJOR_COURESE, majorCourseDBModel.getId(), majorCourseFormModel.getCourseDescriptionModel().getRteModel().getRteLocalMediaList());

                for (ArticleNode articleNode : majorCourseFormModel.getCourseScheduleModel().getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                majorCourseDBModel.setCourseSchedule(JSON.toJSONString(majorCourseFormModel.getCourseScheduleModel()));
                for (ArticleNode articleNode : majorCourseFormModel.getCourseDescriptionModel().getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }

                for (CourseCover courseCover : majorCourseFormModel.getCourseDescriptionModel().getCourseCoverList()) {
                    switch (courseCover.getMediaType()) {
                        case CourseCover.MEDIA_TYPE_PHOTO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                courseCover.setUploadFileId(photo.getId());
                                if (courseCover.isDefaultCover()) {
                                    majorCourseDBModel.setCoverStoreKey(photo.getStoreKey());
                                }
                            }
                            break;
                        }
                        case CourseCover.MEDIA_TYPE_VIDEO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getVideoUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(courseCover.getVideoUrl()))
                                        .parentId(majorCourseDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_MAJOR_COURESE)
                                        .totalSize(courseCover.getSize())
                                        .duration(courseCover.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                courseCover.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                majorCourseDBModel.setCourseDesc(JSON.toJSONString(majorCourseFormModel.getCourseDescriptionModel()));
                MajorCourseDBModelDao.getDao().create(majorCourseDBModel);
                return majorCourseDBModel;
            }
        });
        PublishUtil.startPublish();
        switch (majorCourseFormModel.getOperationType()) {
            case PublishUtil.OPERATION_TYPE_EDIT:
                EventBus.getDefault().post(EventEditMajorCourseFinish.newBuilder()
                        .majorCourseDBModel(result)
                        .resultCode(EventEditMajorCourseFinish.RESULT_OK)
                        .build());
                break;
            case PublishUtil.OPERATION_TYPE_COPY:
                EventBus.getDefault().post(EventCopyMajorCourseFinish.newBuilder()
                        .resultCode(EventCopyMajorCourseFinish.RESULT_OK)
                        .build());
                break;
        }
        return result;
    }

    public static SpecialTopicDBModel publishSpecialTopic(final int publishOperationType, final SpecialTopicFormModel specialTopicFormModel) throws Exception {
        SpecialTopicDBModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<SpecialTopicDBModel>() {
            @Override
            public SpecialTopicDBModel call() throws Exception {
                long releaseDate = System.currentTimeMillis();
                SpecialTopicDBModel specialTopicDBModel = SpecialTopicDBModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .releaseDate(releaseDate)
                        .sellDate(releaseDate)
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .title(specialTopicFormModel.getTitle())
                        .goodsId(specialTopicFormModel.getGoodsId())
                        .location(JSON.toJSONString(specialTopicFormModel.getLocationData()))
                        .coverUrl(specialTopicFormModel.getCoverUrl())
                        .coverNeedUpload(specialTopicFormModel.isCoverNeedUpload())
                        .interest(JSON.toJSONString(specialTopicFormModel.getInterestList()))
                        .industry(JSON.toJSONString(specialTopicFormModel.getIndustryList()))
                        .publishOperationType(publishOperationType)
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .retryCount(0)
                        .router("")
                        .richText(specialTopicFormModel.getRteModel().getContent())
                        .uploadedPercent(0)
                        .build();
                if (specialTopicFormModel.isCoverNeedUpload()) {
                    UploadFile infoCover = UploadFile.newBuilder()
                            .userId(AccountUtil.getCurrentUser().getId())
                            .fileLocalUrl(specialTopicFormModel.getCoverUrl())
                            .storeKey(PublishUtil.generatePhotoFileStoreKey(specialTopicFormModel.getCoverUrl()))
                            .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                            .parentId(specialTopicDBModel.getId())
                            .parentType(PublishUtil.PARENT_TYPE_INFO)
                            .totalSize(FileUtil.getFileSize(specialTopicFormModel.getCoverUrl()))
                            .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                            .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                            .build();
                    UploadFileDao.getDao().create(infoCover);
                    specialTopicDBModel.setCoverStoreKey(infoCover.getStoreKey());
                }
                RichTextUtil.uploadRTELocalMedia(PublishUtil.PARENT_TYPE_INFO, specialTopicDBModel.getId(), specialTopicFormModel.getRteModel().getRteLocalMediaList());
                for (ArticleNode articleNode : specialTopicFormModel.getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(specialTopicDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(specialTopicDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(specialTopicDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                specialTopicDBModel.setArticleJson(JSON.toJSONString(specialTopicFormModel.getArticleNodeList()));
                SpecialTopicDBModelDao.getDao().create(specialTopicDBModel);
                return specialTopicDBModel;
            }
        });
        PublishUtil.startPublish();
        switch (specialTopicFormModel.getOperationType()) {
            case PublishUtil.OPERATION_TYPE_EDIT:
                EventBus.getDefault().post(EventEditSpecialTopicFinish.newBuilder()
                        .specialTopicDBModel(result)
                        .resultCode(EventEditInfoFinish.RESULT_OK)
                        .build());
                break;
            case PublishUtil.OPERATION_TYPE_COPY:
                EventBus.getDefault().post(EventCopySpecialTopicFinish.newBuilder()
                        .resultCode(EventCopyInfoFinish.RESULT_OK)
                        .build());
                break;
        }
        return result;
    }

    public static InfoModel publishInfo(final int publishOperationType, final PublishInfoFormModel publishInfoFormModel) throws Exception {
        InfoModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<InfoModel>() {
            @Override
            public InfoModel call() throws Exception {
                long releaseDate = System.currentTimeMillis();
                InfoModel infoModel = InfoModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .releaseDate(releaseDate)
                        .sellDate(releaseDate)
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .title(publishInfoFormModel.getTitle())
                        .goodsId(publishInfoFormModel.getGoodsId())
                        .location(JSON.toJSONString(publishInfoFormModel.getLocationData()))
                        .coverUrl(publishInfoFormModel.getCoverUrl())
                        .coverNeedUpload(publishInfoFormModel.isCoverNeedUpload())
                        .interest(JSON.toJSONString(publishInfoFormModel.getInterestList()))
                        .industry(JSON.toJSONString(publishInfoFormModel.getIndustryList()))
                        .publishOperationType(publishOperationType)
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .retryCount(0)
                        .router("")
                        .richText(publishInfoFormModel.getRteModel().getContent())
                        .uploadedPercent(0)
                        .build();
                if (publishInfoFormModel.isCoverNeedUpload()) {
                    UploadFile infoCover = UploadFile.newBuilder()
                            .userId(AccountUtil.getCurrentUser().getId())
                            .fileLocalUrl(publishInfoFormModel.getCoverUrl())
                            .storeKey(PublishUtil.generatePhotoFileStoreKey(publishInfoFormModel.getCoverUrl()))
                            .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                            .parentId(infoModel.getId())
                            .parentType(PublishUtil.PARENT_TYPE_INFO)
                            .totalSize(FileUtil.getFileSize(publishInfoFormModel.getCoverUrl()))
                            .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                            .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                            .build();
                    UploadFileDao.getDao().create(infoCover);
                    infoModel.setCoverStoreKey(infoCover.getStoreKey());
                }
                RichTextUtil.uploadRTELocalMedia(PublishUtil.PARENT_TYPE_INFO, infoModel.getId(), publishInfoFormModel.getRteModel().getRteLocalMediaList());
                for (ArticleNode articleNode : publishInfoFormModel.getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(infoModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(infoModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(infoModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_INFO)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                infoModel.setArticleJson(JSON.toJSONString(publishInfoFormModel.getArticleNodeList()));
                InfoModelDao.getDao().create(infoModel);
                return infoModel;
            }
        });
        PublishUtil.startPublish();
        switch (publishInfoFormModel.getOperationType()) {
            case PublishUtil.OPERATION_TYPE_EDIT:
                EventBus.getDefault().post(EventEditInfoFinish.newBuilder()
                        .infoModel(result)
                        .resultCode(EventEditInfoFinish.RESULT_OK)
                        .build());
                break;
            case PublishUtil.OPERATION_TYPE_COPY:
                EventBus.getDefault().post(EventCopyInfoFinish.newBuilder()
                        .resultCode(EventCopyInfoFinish.RESULT_OK)
                        .build());
                break;
        }
        return result;
    }

    public static void jumpToDMPage(Activity activity) {
        Intent intent = new Intent(activity, DMPage.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    public static void jumpToMMPage(Activity activity) {
        Intent intent = new Intent(activity, MMPage.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    public static TraingClassDBModel publishTraingClass(final int publishOperationType, final TraingClassFormModel traingClassFormModel) throws Exception {
        TraingClassDBModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<TraingClassDBModel>() {
            @Override
            public TraingClassDBModel call() throws Exception {
                TraingClassDBModel traingClassDBModel = TraingClassDBModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .releaseDate(System.currentTimeMillis())
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .title(traingClassFormModel.getTitle())
                        .goodsId(traingClassFormModel.getGoodsId())
                        .courseNumber(traingClassFormModel.getCourseNumber())
                        .classLocation(JSON.toJSONString(traingClassFormModel.getClassLocation()))
                        .classSize(traingClassFormModel.getClassSize())
                        .confirmDate(traingClassFormModel.getConfirmDate())
                        .coverUrl(traingClassFormModel.getCover().getImageUrl())
                        .coverNeedUpload(traingClassFormModel.getCover().isNeedUpload())
                        .endClassDate(traingClassFormModel.getEndClassDate())
                        .guarantee(JSON.toJSONString(traingClassFormModel.getGuaranteeItemList()))
                        .interest(JSON.toJSONString(traingClassFormModel.getPublishInterestItem()))
                        .learnTarget(JSON.toJSONString(traingClassFormModel.getSelectLearnTargetItemList()))
                        .minClassSize(traingClassFormModel.getMinClassSize())
                        .openBuy(traingClassFormModel.getOpenBuy())
                        .openClassCondition(traingClassFormModel.getOpenClassCondition())
                        .platformDividend(traingClassFormModel.getPlatformDividend())
                        .price(traingClassFormModel.getPrice())
                        .publishOperationType(publishOperationType)
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .retryCount(0)
                        .router("")
                        .singleClassDuration(traingClassFormModel.getSingleClassDuration())
                        .singlePrice(traingClassFormModel.getSinglePrice())
                        .startClassDate(traingClassFormModel.getStartClassDate())
                        .suitableLearnAge(JSON.toJSONString(traingClassFormModel.getSuitableLearnAgeItemList()))
                        .suitablePeople(JSON.toJSONString(traingClassFormModel.getSelectSuitablePeopleItem()))
                        .tagJson(JSON.toJSONString(traingClassFormModel.getSelectTagItemList()))
                        .traingCard(JSON.toJSONString(traingClassFormModel.getTraingCardList()))
                        .uploadedPercent(0)
                        .build();
                for (ArticleNode articleNode : traingClassFormModel.getCourseScheduleModel().getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                traingClassDBModel.setCourseSchedule(JSON.toJSONString(traingClassFormModel.getCourseScheduleModel()));
                for (ArticleNode articleNode : traingClassFormModel.getCourseDescriptionModel().getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }

                for (CourseCover courseCover : traingClassFormModel.getCourseDescriptionModel().getCourseCoverList()) {
                    switch (courseCover.getMediaType()) {
                        case CourseCover.MEDIA_TYPE_PHOTO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                courseCover.setUploadFileId(photo.getId());
                                if (courseCover.isDefaultCover()) {
                                    traingClassDBModel.setCoverStoreKey(photo.getStoreKey());
                                }
                            }
                            break;
                        }
                        case CourseCover.MEDIA_TYPE_VIDEO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getVideoUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(courseCover.getVideoUrl()))
                                        .parentId(traingClassDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CLASS)
                                        .totalSize(courseCover.getSize())
                                        .duration(courseCover.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                courseCover.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                traingClassDBModel.setCourseDesc(JSON.toJSONString(traingClassFormModel.getCourseDescriptionModel()));
                TraingClassDBModelDao.getDao().create(traingClassDBModel);
                return traingClassDBModel;
            }
        });
        PublishUtil.startPublish();
        switch (traingClassFormModel.getOperationType()) {
            case PublishUtil.OPERATION_TYPE_EDIT:
                EventBus.getDefault().post(EventEditTraingClassFinish.newBuilder()
                        .traingClassDBModel(result)
                        .resultCode(EventEditTraingClassFinish.RESULT_OK)
                        .build());
                break;
            case PublishUtil.OPERATION_TYPE_COPY:
                EventBus.getDefault().post(EventCopyTraingClassFinish.newBuilder()
                        .resultCode(EventCopyTraingClassFinish.RESULT_OK)
                        .build());
                break;
        }
        return result;
    }

    public static TraingCardDBModel publishTraingCard(final int publishOperationType, final TraingCardFormModel traingCardFormModel) throws Exception {
        TraingCardDBModel result = TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<TraingCardDBModel>() {
            @Override
            public TraingCardDBModel call() throws Exception {
                TraingCardDBModel traingCardDBModel = TraingCardDBModel.newBuilder()
                        .id(UUIDUtil.generate())
                        .userId(AccountUtil.getCurrentUser().getId())
                        .goodsId(traingCardFormModel.getGoodsId())
                        .title(traingCardFormModel.getTitle())
                        .cardType(traingCardFormModel.getCardType())
                        .usageCount(traingCardFormModel.getUsageCount())
                        .effectiveType(traingCardFormModel.getEffectiveType())
                        .validity(traingCardFormModel.getValidity())
                        .price(traingCardFormModel.getValidity())
                        .platformDividend(traingCardFormModel.getPlatformDividend())
                        .tagJson(JSON.toJSONString(traingCardFormModel.getSelectTagItemList()))
                        .course(JSON.toJSONString(traingCardFormModel.getCourseList()))
                        .coverUrl(traingCardFormModel.getCover().getImageUrl())
                        .coverNeedUpload(traingCardFormModel.getCover().isNeedUpload())
                        .publishOperationType(publishOperationType)
                        .guarantee(JSON.toJSONString(traingCardFormModel.getGuaranteeItemList()))
                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                        .releaseDate(System.currentTimeMillis())
                        .router("")
                        .uploadedPercent(0)
                        .build();
                for (ArticleNode articleNode : traingCardFormModel.getCourseDescriptionModel().getArticleNodeList()) {
                    switch (articleNode.getType()) {
                        case ArticleNode.NODE_TYPE_PHOTO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(articleNode.getUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getUrl()))
                                        .description(articleNode.getText())
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(articleNode.getUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                articleNode.setUploadFileId(photo.getId());
                            }
                            break;
                        }
                        case ArticleNode.NODE_TYPE_VIDEO: {
                            if (articleNode.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getCoverUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(articleNode.getCoverUrl()))
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .totalSize(FileUtil.getFileSize(articleNode.getCoverUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(articleNode.getUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(articleNode.getUrl()))
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .totalSize(articleNode.getSize())
                                        .duration(articleNode.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                articleNode.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }

                for (CourseCover courseCover : traingCardFormModel.getCourseDescriptionModel().getCourseCoverList()) {
                    switch (courseCover.getMediaType()) {
                        case CourseCover.MEDIA_TYPE_PHOTO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile photo = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .build();
                                UploadFileDao.getDao().create(photo);
                                courseCover.setUploadFileId(photo.getId());
                                if (courseCover.isDefaultCover()) {
                                    traingCardDBModel.setCoverStoreKey(photo.getStoreKey());
                                }
                            }
                            break;
                        }
                        case CourseCover.MEDIA_TYPE_VIDEO: {
                            if (courseCover.isNeedUpload()) {
                                UploadFile videoCover = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getImageUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                                        .storeKey(PublishUtil.generatePhotoFileStoreKey(courseCover.getImageUrl()))
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .totalSize(FileUtil.getFileSize(courseCover.getImageUrl()))
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_WAITING_UPLOAD)
                                        .build();
                                UploadFileDao.getDao().create(videoCover);
                                UploadFile video = UploadFile.newBuilder()
                                        .userId(AccountUtil.getCurrentUser().getId())
                                        .fileLocalUrl(courseCover.getVideoUrl())
                                        .fileType(PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                                        .storeKey(PublishUtil.generateVideoFileStoreKey(courseCover.getVideoUrl()))
                                        .parentId(traingCardDBModel.getId())
                                        .parentType(PublishUtil.PARENT_TYPE_TRAING_CARD)
                                        .totalSize(courseCover.getSize())
                                        .duration(courseCover.getDuration())
                                        .controlOption(PublishUtil.CONTROL_OPTION_UPLOAD)
                                        .progressStatus(PublishUtil.PROGRESS_STATUS_NEED_TRANSCODE)
                                        .coverStoreKey(videoCover.getStoreKey())
                                        .coverUrl(videoCover.getFileLocalUrl())
                                        .build();
                                UploadFileDao.getDao().create(video);
                                courseCover.setUploadFileId(video.getId());
                            }
                            break;
                        }
                    }
                }
                traingCardDBModel.setCourseDesc(JSON.toJSONString(traingCardFormModel.getCourseDescriptionModel()));
                TraingCardDBModelDao.getDao().create(traingCardDBModel);
                return traingCardDBModel;
            }
        });
        PublishUtil.startPublish();
        switch (traingCardFormModel.getOperationType()) {
            case PublishUtil.OPERATION_TYPE_EDIT:
                EventBus.getDefault().post(EventEditTraingCardFinish.newBuilder()
                        .traingCardDBModel(result)
                        .resultCode(EventEditTraingCardFinish.RESULT_OK)
                        .build());
                break;
            case PublishUtil.OPERATION_TYPE_COPY:
                EventBus.getDefault().post(EventCopyTraingCardFinish.newBuilder()
                        .resultCode(EventEditTraingCardFinish.RESULT_OK)
                        .build());
                break;
        }
        return result;
    }
}
